package com.wl.java_spring_boot01.modules.account.dao;

import com.wl.java_spring_boot01.modules.account.entity.Role;
import com.wl.java_spring_boot01.modules.common.vo.SearchVo;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author 汪林
 * @date 2020/8/21 12:09
 */
@Repository
@Mapper
public interface RoleDao {
    //查询--注意拼接时候的空格
    @Select("select * from role r " +
            "left join user_role ur on r.role_id =ur.role_id" +
            " where ur.user_id = #{userId} ")
    List<Role> getRolesByUserId(int userId);

    //查询所有角色
    @Select("select * from role")
    List<Role> getRoles();

    //分页查询
    @Select("<script>" +
            "select * from role "
            + "<where> "
            + "<if test='keyWord != \"\" and keyWord != null'>"
            + " and (role_name like '%${keyWord}%') "
            + "</if>"
            + "</where>"
            + "<choose>"
            + "<when test='orderBy != \"\" and orderBy != null'>"
            + " order by ${orderBy} ${sort}"
            + "</when>"
            + "<otherwise>"
            + " order by role_id desc"
            + "</otherwise>"
            + "</choose>"
            + "</script>")
    List<Role> getUsersBySearchVo(SearchVo searchVo);

    //增加
    @Insert("insert into role (role_name) " +
            "values (#{roleName})")
    @Options(useGeneratedKeys = true, keyColumn = "role_id", keyProperty = "roleId")
    void insertRole(Role role);

    //按name查找
    @Select("select * from role where role_name = #{roleName}")
    Role getRoleByRoleName(String roleName);

    //id查找
    @Select("select * from role where role_id = #{roleId}")
    Role getRoleByRoleId(int roleId);

    //修改
    @Update("update role set role_name =#{roleName} where role_id =#{roleId}")
    void updateRole(Role role);

    //删除
    @Delete("delete from role where role_id = #{role_id}")
    void deleteUser(int roleId);
}
